(ns config.migrate_config
  (:require [clojure.contrib.sql :as sql]))

(def version (atom nil))

(defn current-db-version-fn []
  (or @version 0))

(defn update-db-version-fn [new-version]
  (reset! version new-version))

(defn migrate-config []
  { :directory "/src/migrations"
   :current-version current-db-version-fn
   :update-version update-db-version-fn })

(defn db-version []
  (sql/with-connection DB
    (sql/with-query-results res 
      ["select version from schema_migrations limit 1"]
      (:version (first res)))))

(defn update-db-version [version]
  (sql/with-connection DB
    (sql/insert-values :schema_migrations [:version] [version])))

